package top.zhangjianyong.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池持有
 *
 * @author zhangjianyong
 * @since 2024/6/15 下午2:48
 */
public class ThreadPoolManage {

    private static ThreadPoolExecutor POOL;
    private final static CountDownLatch latch = new CountDownLatch(1);

    public ThreadPoolManage(ThreadPoolConfig threadPoolConfig) {
        POOL = new ThreadPoolExecutor(threadPoolConfig.getCorePoolSize(), threadPoolConfig.getMaxPoolSize(), threadPoolConfig.getKeepAliveTime(), threadPoolConfig.getTimeUnit(), new LinkedBlockingQueue<>(threadPoolConfig.getQueueCapacity()));
    }

    public void execute(Runnable runnable) {
        POOL.execute(() -> {
            try {
                runnable.run();
            } finally {
                latch.countDown();
            }
        });
    }

    public static void main(String[] args) throws InterruptedException {

        CountDownLatch latch = new CountDownLatch(11);
        latch.countDown();
        latch.countDown();
        latch.countDown();
        System.out.println(latch.getCount());
    }

}
